<!--left1 出单量占比-->
<template>
    <div class="left1">
      <!-- 中间的“AA”和小三角 -->
      <div class="title-box" :style="{ left: (titleLeftPositionPx ) + 'px' }">
        <span class="title">车险</span>
        <div class="triangle"></div>
      </div>

      <!-- 模拟柱状图 -->
      <ul class="chart">
        <li 
          v-for="(item, index) in useData" 
          :key="index"
          :ref="el => liRefs[index] = el"
          :style="item.rate > 0 || sum === 0 ? { width: item.ratePercentage } : { display: 'none' }"
        >
          <template v-if="index % 2 === 0">
            <span class="chart__title--left" v-html="item.title" v-if="!item.isNarrow"></span>
            <span class="chart__title--left--outside" v-html="item.title2" v-if="item.isNarrow"></span> 
            <span class="chart__rate">{{ item.rateFixed }}%</span>
          </template>
          <template v-else>
            <span class="chart__rate">{{ item.rateFixed }}%</span>
            <span class="chart__title--right" v-html="item.title" v-if="!item.isNarrow"></span> 
            <span class="chart__title--right--outside" v-html="item.title2" v-if="item.isNarrow"></span> 
          </template>
        </li>
      </ul>

      <ul class="chart">
        <li 
          v-for="(item, index) in useData2" 
          :key="index"
          :style="item.rate > 0 || sum === 0 ? { width: item.ratePercentage } : { display: 'none' }"
        >
          <template v-if="index % 2 === 0">
            <span class="chart__title--left" v-html="item.title" v-if="!item.isNarrow"></span>
            <span class="chart__title--left--outside" v-html="item.title2" v-if="item.isNarrow"></span> 
            <span class="chart__rate">{{ item.rateFixed }}%</span>
          </template>
          <template v-else>
            <span class="chart__rate">{{ item.rateFixed }}%</span>
            <span class="chart__title--right" v-html="item.title" v-if="!item.isNarrow"></span> 
            <span class="chart__title--right--outside" v-html="item.title2" v-if="item.isNarrow"></span> 
          </template>
        </li>
      </ul>
    </div>
</template>
<script setup>
import { ref, computed, watch, nextTick, onMounted } from 'vue'

// 模拟数据
const data = ref([
  { title: '新保出单量', value: Math.round(Math.random() * 100) },
  { title: '续保出单量', value: Math.round(Math.random() * 100) },
])

const data2 = ref([
  { title: '在线单量', value: Math.round(Math.random() * 100) },
  { title: 'OCR', value: Math.round(Math.random() * 100) },
])

const sum = computed(() => data.value.reduce((acc, cur) => acc + cur.value, 0))

const useData = computed(() => {
  return data.value.map(item => {
    const rate = sum.value ? item.value / sum.value : 0
    return {
      title: item.title,
      title2:item.title.replace(/(.{3})/, "$1<br/>"),
      rate,
      ratePercentage: sum.value === 0 ? '50%' : (rate * 100).toFixed(2) + '%',
      rateFixed: (rate * 100).toFixed(),
      isNarrow: rate <= 0.2,
    }
  })
})
const useData2 = computed(() => {
    const sum=data2.value.reduce((acc, cur) => acc + cur.value, 0)
    return data2.value.map(item => {
        const rate = sum ? item.value / sum : 0
        return {
        title: item.title,
        title2:item.title.replace(/(.{3})/, "$1<br/>"),
        rate,
        ratePercentage: sum === 0 ? '50%' : (rate * 100).toFixed(2) + '%',
        rateFixed: (rate * 100).toFixed(),
        }
    })
})



const liRefs = ref([])
const titleLeftPositionPx = ref(0)

// 计算中间位置：左边li.offsetLeft + offsetWidth
const calcMiddlePosition = () => {
  nextTick(() => {
    if (liRefs.value.length >= 2 && useData.value.length > 0) {
      const left = liRefs.value[0]
      const leftRate = useData.value[0].rate  // 获取左边占比

      if (leftRate <= 0.05 || leftRate >= 0.95) {
        // 若太小或太大，强制居中显示
        const wrapper = left?.offsetParent
        if (wrapper) {
          titleLeftPositionPx.value = wrapper.offsetWidth / 2
        }
      } else {
        // 正常情况：三角跟随左边 li 的右边界
        const offset = left.offsetLeft + left.offsetWidth
        titleLeftPositionPx.value = offset
      }
    }
  })
}


// 监听数据变化
watch(
    useData, 
    () => {
        calcMiddlePosition()
    },
)

// 初始计算
onMounted(() => {
  calcMiddlePosition()
})
</script>

<style lang="scss" scoped>

// $color-blue: #18267f;
// $color-right: #800d10;
$color-blue: #2d79f3;
$color-right: #68d13f;
$font-size: 1rem;


.left1{
    position: relative;
    margin-top: 1rem;
    width: 100%;
    height: 9rem;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
    .title-box {
        position: absolute;
        top: 5%;
        transform: translateX(-50%);
        text-align: center;
        .title {  font-size: $font-size; }

        .triangle {
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            top: calc(100% + 2px);
            width: 0;
            height: 0;
            border-left: 4px solid transparent;
            border-right: 4px solid transparent;
            border-top: 4px solid gold;
        }
    }

    .chart {
        // position: relative;
        width: 85%;
        height: 3.4rem;
        display: flex;
        flex-direction: row;
        margin: 0;
        padding: 0;
        margin-bottom: .8rem;
        &:nth-of-type(2){
            width: 55%;
            height:2.5rem;
            margin-bottom: 0;
            .chart__rate{
                font-size: 1rem;
            }
            .chart__title{
                font-size: .6rem;
            }
        }
        // margin-bottom: 10px;
        li {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: $color-blue;
            border-radius: 1.6rem;
            padding: 0 .8rem;

        &:nth-of-type(1) {
            border-top-right-radius: .5rem;
            border-bottom-right-radius: 0;
        }

        &:nth-last-of-type(1) {
            background-color: $color-right;
            border-top-left-radius: .5rem;
            border-bottom-left-radius: 0;
            border-left: 2px solid #000;
        }
        }

        &__title {
        &--left, &--right {
            font-size:$font-size;
            font-weight: 100;
            color: #fff;
        }

        &--left {
            text-align: left;
            &--outside{
                font-size: .8rem;
                font-weight: 100;
                text-align: right ;
                margin-left: -3.5rem;
            }
        }

        &--right {
            text-align: right;
            &--outside{
                font-size: .8rem;
                font-weight: 100;
                margin-right:-3.5rem;
                text-align: left;
            }
        }
        }

        &__rate {
            color: #fff;
            font-size: 2rem;
            font-weight: 600;
        }
    } 
}


</style>